package com.glp.work.flow.service.impl;

import com.glp.common.reponse.BaseResult;
import com.glp.work.flow.constant.APICode;
import com.glp.work.flow.constant.Constants;
import com.glp.work.flow.dto.ProcTaskInfoDTO;
import com.glp.work.flow.dto.UserEntity;
import com.glp.work.flow.dto.request.*;
import com.glp.work.flow.exception.APIException;
import com.glp.work.flow.mapper.*;
import com.glp.work.flow.pojo.*;
import com.glp.work.flow.service.client.PermissionClient;
import com.glp.work.flow.service.UserTaskAssignService;
import com.glp.work.flow.utils.DateUtils;
import com.glp.work.flow.workflow.plugin.flowable.service.FlowProcessDefinitionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.flowable.bpmn.model.UserTask;
import org.flowable.editor.language.json.converter.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户任务分配
 *
 * @author pengjia
 * @version 2020年5月18日 下午1:46:36
 */
@Service
@Slf4j
public class UserTaskAssignServiceImpl implements UserTaskAssignService {

    @Value("${glp.fbs-app.appId}")
    private String appId;
    @Autowired
    private WfTaskAssignMapper wfTaskAssignMapper;
    @Autowired
    private WfTaskAssignUsersMapper wfTaskAssignUsersMapper;
    @Autowired
    private WfTaskAssignNodeMapper wfTaskAssignNodeMapper;
    @Autowired
    private PermissionClient permissionService;
    @Autowired
    private FlowProcessDefinitionService flowProcessDefinitionService;
    @Autowired
    private WfTaskAssignRoleMapper wfTaskAssignRoleMapper;
    @Autowired
    private WfOrderMapper wfOrderMapper;
    @Resource
    private WfTaskSubscriptionUsersMapper wfTaskSubscriptionUsersMapper;
    @Resource
    private WfTaskSubscriptionRoleMapper wfTaskSubscriptionRoleMapper;

    @Override
    public List<AssignUserInfoDTO> queryAllUserInfo() {
        UsersRequstDto dto = new UsersRequstDto();
        dto.setAppId(appId);
        List<UserEntity> userEntityList = null;
        try {
            BaseResult<List<UserEntity>> listResult = permissionService.userList(dto);
            userEntityList = listResult.getData();
        } catch (Exception e) {
            log.error("根据APPID查询用户信息异常！{}", appId, e);
            throw new APIException(APICode.DATA_ERROR, "根据APPID查询用户信息异常！" + appId);
        }
        List<AssignUserInfoDTO> resultList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userEntityList)) {
            for (UserEntity source : userEntityList) {
                AssignUserInfoDTO target = new AssignUserInfoDTO();
                target.setUserName(source.getUserName());
                target.setUserNo(source.getUserNo());
                resultList.add(target);
            }
        }
        return resultList;
    }


    //查询任务分配下的信息
    @Override
    public List<ProcTaskInfoDTO> queryProAllTaskDefInfo(ProDefDTO request) {
        log.info("查询流程定义节点信息。proDefid：{}", request.getProDefId());
        List<UserTask> userTaskList = flowProcessDefinitionService.getUserTaskListById(request.getProDefId());
        log.info("userTaskList信息：{}", userTaskList);
        List<ProcTaskInfoDTO> taskTargetList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userTaskList)) {
            for (UserTask taskSource : userTaskList) {
                ProcTaskInfoDTO targetTask = new ProcTaskInfoDTO();
                targetTask.setTaskDefId(taskSource.getId());
                targetTask.setTaskDefKey(taskSource.getName());
                Example example = new Example(WfTaskAssign.class);
                example.createCriteria()
                        .andEqualTo("procDefId", request.getProDefId())
                        .andEqualTo("taskDefId", taskSource.getId());
                WfTaskAssign main = wfTaskAssignMapper.selectOneByExample(example);
                if (main != null) {
                    log.info("根据流程定义id，和任务定义id，得到任务分配表信息：{}", main);
                    targetTask.setCreateTime(main.getCreateTime());
                    targetTask.setCreateUser(main.getCreateUser());
                    targetTask.setUpdateTime(main.getUpdateTime());
                    targetTask.setUpdateUser(main.getUpdateUser());
                    targetTask.setTaskReceiveList(getTaskReceiveUsers(main));
                    log.info("根据任务分配表进行查询，被分配用户信息{}", targetTask.getTaskReceiveList());
                    targetTask.setCreateTime(main.getCreateTime());
                    targetTask.setCreateUser(main.getCreateUser());
                    targetTask.setUpdateTime(main.getUpdateTime());
                    targetTask.setUpdateUser(main.getUpdateUser());
                    targetTask.setTaskRoleReceiveList(getTaskReceiveRolrs(main));
                    log.info("根据任务分配表进行查询，被分配角色信息{}", targetTask.getTaskRoleReceiveList());
                }

                taskTargetList.add(targetTask);
            }
        }
        return taskTargetList;
    }

    //查询任务分配下的信息
    @Override
    public List<ProcTaskInfoDTO> queryProAllTaskDefInfoNew(ProDefDTO request) {
        log.info("查询流程定义节点信息。proDefid：{}", request.getProDefId());
        List<UserTask> userTaskList = flowProcessDefinitionService.getUserTaskListById(request.getProDefId());
        log.info("userTaskList信息{}：", userTaskList);
        List<ProcTaskInfoDTO> taskTargetList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userTaskList)) {
            for (UserTask taskSource : userTaskList) {
                ProcTaskInfoDTO targetTask = new ProcTaskInfoDTO();
                targetTask.setTaskDefId(taskSource.getId());
                targetTask.setTaskDefKey(taskSource.getName());
                Example example = new Example(WfTaskAssign.class);
                example.createCriteria()
                        .andEqualTo("procDefId", request.getProDefId())
                        .andEqualTo("taskDefId", taskSource.getId());
                WfTaskAssign main = wfTaskAssignMapper.selectOneByExample(example);
                if (main != null) {
                    targetTask.setCreateTime(main.getCreateTime());
                    targetTask.setCreateUser(main.getCreateUser());
                    targetTask.setUpdateTime(main.getUpdateTime());
                    targetTask.setUpdateUser(main.getUpdateUser());
                    if ("1".equals(main.getType())) {
                        targetTask.setTaskReceiveList(getTaskReceiveUsers(main));
                        log.info("根据任务分配表进行查询，被分配用户信息{}", targetTask.getTaskReceiveList());
                    } else {
                        targetTask.setTaskRoleReceiveList(getTaskReceiveRolrs(main));
                        log.info("根据任务分配表进行查询，被分配角色信息{}", targetTask.getTaskRoleReceiveList());
                    }
                    targetTask.setTaskDefRule(main.getTaskDefRule());
                    targetTask.setTaskDefBatch(main.getTaskDefBatch());
                    targetTask.setTaskDefForm(main.getTaskDefForm());
                    targetTask.setType(main.getType());
                    targetTask.setNotificationType(main.getNotificationType());
                    targetTask.setNotification(main.getNotification());
                    targetTask.setNoticeCreateUserSwitch(main.getNoticeCreateUserSwitch());
                    targetTask.setSubscriptionRoleInfos(getTaskSubscriptionRoles(main));
                    targetTask.setSubscriptionUserInfos(getTaskSubscriptionUsers(main));
                    targetTask.setSms(main.getSms());
                    if (main.getOptionType() != null) {
                        targetTask.setOptionType(Arrays.asList(main.getOptionType().split(",")));
                    }
                    if (StringUtils.isNotEmpty(main.getTaskDefTtpe())) {
                        targetTask.setTaskDefTtpe(main.getTaskDefTtpe());
                    } else {
                        targetTask.setTaskDefTtpe("userTask");
                    }
                    targetTask.setNodeInfos(getTaskAssignNode(main));
                } else {
                    targetTask.setTaskDefTtpe("userTask");
                }

                taskTargetList.add(targetTask);
            }
        }
        return taskTargetList;
    }

    @Override
    public WfTaskAssign setlectByTaskDefId(String procDefId, String taskDefinitionKey) {
        Example example = new Example(WfTaskAssign.class);
        example.createCriteria()
                .andEqualTo("procDefId", procDefId)
                .andEqualTo("taskDefId", taskDefinitionKey);
        List<WfTaskAssign> wfTaskAssigns = wfTaskAssignMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(wfTaskAssigns)) {
            return wfTaskAssigns.get(0);
        }
        return null;
    }

    private List<ProcTaskAssignRoleInfoDTO> getTaskSubscriptionRoles(WfTaskAssign main) {
        List<ProcTaskAssignRoleInfoDTO> result = new ArrayList<>();
        if (main != null) {
            Example subExample = new Example(WfTaskAssignUsers.class);
            subExample.createCriteria().andEqualTo("mainId", main.getId());
            List<WfTaskSubscriptionRole> wfTaskSubscriptionRoles = wfTaskSubscriptionRoleMapper.selectByExample(subExample);
            if (CollectionUtils.isNotEmpty(wfTaskSubscriptionRoles)) {
                for (WfTaskSubscriptionRole source : wfTaskSubscriptionRoles) {
                    ProcTaskAssignRoleInfoDTO target = new ProcTaskAssignRoleInfoDTO();
                    target.setRoleName(source.getRoleName());
                    target.setRoleCode(source.getRoleCode());
                    result.add(target);
                }
            }
        }
        return result;
    }

    private List<ProcTaskAssignUserInfoDTO> getTaskSubscriptionUsers(WfTaskAssign main) {
        List<ProcTaskAssignUserInfoDTO> result = new ArrayList<>();
        if (main != null) {
            Example subExample = new Example(WfTaskAssignUsers.class);
            subExample.createCriteria().andEqualTo("mainId", main.getId());
            List<WfTaskSubscriptionUsers> wfTaskSubscriptionUsers = wfTaskSubscriptionUsersMapper.selectByExample(subExample);
            if (CollectionUtils.isNotEmpty(wfTaskSubscriptionUsers)) {
                for (WfTaskSubscriptionUsers source : wfTaskSubscriptionUsers) {
                    ProcTaskAssignUserInfoDTO target = new ProcTaskAssignUserInfoDTO();
                    target.setUserName(source.getUserName());
                    target.setUserNo(source.getUserNo());
                    result.add(target);
                }
            }
        }
        return result;
    }

    /**
     * 根据流程定义ID、任务定义ID，查询任务分配用户子表信息
     */
    private List<ProcTaskAssignUserInfoDTO> getTaskReceiveUsers(WfTaskAssign main) {
        List<ProcTaskAssignUserInfoDTO> result = new ArrayList<>();
        if (main != null) {
            Example subExample = new Example(WfTaskAssignUsers.class);
            subExample.createCriteria().andEqualTo("mainId", main.getId());
            List<WfTaskAssignUsers> taskUserList = wfTaskAssignUsersMapper.selectByExample(subExample);
            if (CollectionUtils.isNotEmpty(taskUserList)) {
                for (WfTaskAssignUsers source : taskUserList) {
                    ProcTaskAssignUserInfoDTO target = new ProcTaskAssignUserInfoDTO();
                    target.setUserName(source.getUserName());
                    target.setUserNo(source.getUserNo());
                    result.add(target);
                }
            }
        }
        return result;
    }

    /**
     * 根据流程定义ID、任务定义ID，查询任务分配角色子表信息
     */
    private List<ProcTaskAssignRoleInfoDTO> getTaskReceiveRolrs(WfTaskAssign main) {
        List<ProcTaskAssignRoleInfoDTO> result = new ArrayList<>();
        if (main != null) {
            Example subExample = new Example(WfTaskAssignRole.class);
            subExample.createCriteria().andEqualTo("mainId", main.getId());
            List<WfTaskAssignRole> wfTaskAssignRoles = wfTaskAssignRoleMapper.selectByExample(subExample);
            if (CollectionUtils.isNotEmpty(wfTaskAssignRoles)) {
                for (WfTaskAssignRole source : wfTaskAssignRoles) {
                    ProcTaskAssignRoleInfoDTO target = new ProcTaskAssignRoleInfoDTO();
                    target.setRoleCode(source.getRoleCode());
                    target.setRoleName(source.getRoleName());
                    result.add(target);
                }
            }
        }
        return result;
    }

    /**
     * 根据流程定义ID、任务定义ID，查询任务可驳回节点信息
     */
    private List<ProcTaskNodeInfoDTO> getTaskAssignNode(WfTaskAssign main) {
        List<ProcTaskNodeInfoDTO> result = new ArrayList<>();
        if (main != null) {
            Example subExample = new Example(WfTaskAssignNode.class);
            subExample.createCriteria().andEqualTo("mainId", main.getId());
            List<WfTaskAssignNode> wfTaskAssignNodes = wfTaskAssignNodeMapper.selectByExample(subExample);
            if (CollectionUtils.isNotEmpty(wfTaskAssignNodes)) {
                for (WfTaskAssignNode wfTaskAssignNode : wfTaskAssignNodes) {
                    ProcTaskNodeInfoDTO procTaskNodeInfoDTO = new ProcTaskNodeInfoDTO();
                    procTaskNodeInfoDTO.setTaskDefId(wfTaskAssignNode.getNodeId());
                    procTaskNodeInfoDTO.setTaskDefKey(wfTaskAssignNode.getNodeName());
                    result.add(procTaskNodeInfoDTO);
                }
            }
        }
        return result;
    }

    //保存任务分配信息1.0
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveTaskAssignInfo(ProcTaskAssignInfoDTO procTaskAssignInfoDTO) {

        log.info("更新任务分配主表信息，流程定义ID：{}，任务定义KEY：{}", procTaskAssignInfoDTO.getProcDefId(), procTaskAssignInfoDTO.getTaskDefKey());
        WfTaskAssign taskAssign = insertOrUpdateWfTaskAssign(procTaskAssignInfoDTO);

        log.info("更新任务分配子表信息，主表ID：{}", taskAssign.getId());
        updateWfTaskAssignUserOrRole(procTaskAssignInfoDTO, taskAssign);
    }

    //保存任务分配信息2.0
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveTaskAssignInfoNew(ProcTaskAssignInfoDTO procTaskAssignInfoDTO) {

        log.info("更新任务分配主表信息，流程定义ID：{}，任务定义KEY：{}", procTaskAssignInfoDTO.getProcDefId(), procTaskAssignInfoDTO.getTaskDefKey());
        WfTaskAssign taskAssign = insertOrUpdateWfTaskAssignNew(procTaskAssignInfoDTO);

        log.info("更新任务分配子表信息，主表ID：{}", taskAssign.getId());
        updateWfTaskAssignUserOrRoleNew(procTaskAssignInfoDTO, taskAssign);

        log.info("更新消息订阅子表信息，主表ID：{}", taskAssign.getId());
        updateWfTaskSubscriptionUserOrRoleNew(procTaskAssignInfoDTO, taskAssign);
    }


    /***
     * @Description: 添加或者修改主表数据
     * @Param: [procTaskAssignInfoDTO]
     * @return: com.glp.work.flow.pojo.WfTaskAssign
     * @Author: 郭永伟
     * @Date: 2020/8/10
     */
    private WfTaskAssign insertOrUpdateWfTaskAssign(ProcTaskAssignInfoDTO procTaskAssignInfoDTO) {
        Example example1 = new Example(WfTaskAssign.class);
        example1.createCriteria()
                .andEqualTo("procDefKey", procTaskAssignInfoDTO.getProcDefKey())
                .andEqualTo("taskDefKey", procTaskAssignInfoDTO.getTaskDefKey());
        WfTaskAssign taskAssign = wfTaskAssignMapper.selectOneByExample(example1);
        if (taskAssign == null) {
            taskAssign = new WfTaskAssign();
            taskAssign.setProcDefId(procTaskAssignInfoDTO.getProcDefId());
            taskAssign.setProcDefKey(procTaskAssignInfoDTO.getProcDefKey());
            taskAssign.setTaskDefId(procTaskAssignInfoDTO.getTaskDefId());
            taskAssign.setTaskDefKey(procTaskAssignInfoDTO.getTaskDefKey());
            taskAssign.setCreateUser(procTaskAssignInfoDTO.getUserNo());
            taskAssign.setCreateTime(DateUtils.getCurrent());
            taskAssign.setType(procTaskAssignInfoDTO.getType());
            taskAssign.setNotification(procTaskAssignInfoDTO.getNotification());
            taskAssign.setNotificationType(procTaskAssignInfoDTO.getNotificationType());
            taskAssign.setNoticeCreateUserSwitch(procTaskAssignInfoDTO.getNoticeCreateUserSwitch());
            wfTaskAssignMapper.insertSelective(taskAssign);
        } else {
            taskAssign.setUpdateTime(DateUtils.getCurrent());
            taskAssign.setUpdateUser(procTaskAssignInfoDTO.getUserNo());
            taskAssign.setType(procTaskAssignInfoDTO.getType());
            taskAssign.setProcDefId(procTaskAssignInfoDTO.getProcDefId());
            wfTaskAssignMapper.updateByPrimaryKeySelective(taskAssign);
        }
        return taskAssign;
    }

    /***
     * @Description: 添加或者修改主表数据
     * @Param: [procTaskAssignInfoDTO]
     * @return: com.glp.work.flow.pojo.WfTaskAssign
     * @Author: 金传奇
     * @Date: 2020/9/26
     */
    private WfTaskAssign insertOrUpdateWfTaskAssignNew(ProcTaskAssignInfoDTO procTaskAssignInfoDTO) {
        Example example1 = new Example(WfTaskAssign.class);
        example1.createCriteria()
                .andEqualTo("procDefKey", procTaskAssignInfoDTO.getProcDefKey())
                .andEqualTo("procDefId", procTaskAssignInfoDTO.getProcDefId())
                .andEqualTo("taskDefId", procTaskAssignInfoDTO.getTaskDefId());
        WfTaskAssign taskAssign = wfTaskAssignMapper.selectOneByExample(example1);
        if (taskAssign == null) {
            taskAssign = new WfTaskAssign();
            taskAssign.setProcDefId(procTaskAssignInfoDTO.getProcDefId());
            taskAssign.setProcDefKey(procTaskAssignInfoDTO.getProcDefKey());
            taskAssign.setTaskDefId(procTaskAssignInfoDTO.getTaskDefId());
            taskAssign.setTaskDefKey(procTaskAssignInfoDTO.getTaskDefKey());
            taskAssign.setCreateUser(procTaskAssignInfoDTO.getUserNo());
            taskAssign.setCreateTime(DateUtils.getCurrent());
            taskAssign.setType(procTaskAssignInfoDTO.getType());
            taskAssign.setTaskDefForm(procTaskAssignInfoDTO.getTaskDefForm());
            taskAssign.setNotification(procTaskAssignInfoDTO.getNotification());
            taskAssign.setNoticeCreateUserSwitch(procTaskAssignInfoDTO.getNoticeCreateUserSwitch());
            taskAssign.setNotificationType(procTaskAssignInfoDTO.getNotificationType());
            if (StringUtils.isNotEmpty(procTaskAssignInfoDTO.getTaskDefRule())) {
                taskAssign.setTaskDefRule(procTaskAssignInfoDTO.getTaskDefRule());
            }
            taskAssign.setTaskDefBatch(procTaskAssignInfoDTO.getTaskDefBatch());
            taskAssign.setTaskDefTtpe(procTaskAssignInfoDTO.getTaskDefTtpe());
            taskAssign.setSms(procTaskAssignInfoDTO.getSms());
            taskAssign.setOptionType(procTaskAssignInfoDTO.getOptionType());
            wfTaskAssignMapper.insertSelective(taskAssign);
        } else {
            taskAssign.setUpdateTime(DateUtils.getCurrent());
            taskAssign.setUpdateUser(procTaskAssignInfoDTO.getUserNo());
            taskAssign.setType(procTaskAssignInfoDTO.getType());
            taskAssign.setProcDefId(procTaskAssignInfoDTO.getProcDefId());
            taskAssign.setTaskDefForm(procTaskAssignInfoDTO.getTaskDefForm());
            taskAssign.setNotification(procTaskAssignInfoDTO.getNotification());
            if (StringUtils.isNotEmpty(procTaskAssignInfoDTO.getTaskDefRule())) {
                taskAssign.setTaskDefRule(procTaskAssignInfoDTO.getTaskDefRule());
            }
            taskAssign.setTaskDefBatch(procTaskAssignInfoDTO.getTaskDefBatch());
            taskAssign.setNoticeCreateUserSwitch(procTaskAssignInfoDTO.getNoticeCreateUserSwitch());
            taskAssign.setNotificationType(procTaskAssignInfoDTO.getNotificationType());
            taskAssign.setTaskDefTtpe(procTaskAssignInfoDTO.getTaskDefTtpe());
            taskAssign.setSms(procTaskAssignInfoDTO.getSms());
            taskAssign.setOptionType(procTaskAssignInfoDTO.getOptionType());
            wfTaskAssignMapper.updateByPrimaryKeySelective(taskAssign);
        }
        return taskAssign;
    }

    /***
     * @Description: 根据类型更新子表信息
     * @Param: [procTaskAssignInfoDTO, taskAssign]
     * @return: void
     * @Author: 郭永伟
     * @Date: 2020/8/10
     */
    private void updateWfTaskAssignUserOrRole(ProcTaskAssignInfoDTO procTaskAssignInfoDTO, WfTaskAssign taskAssign) {
        //1是用户类型
        if (Constants.STATUS_1.equals(procTaskAssignInfoDTO.getType())) {
            log.info("删除用户子表信息，主表ID：{}", taskAssign.getId());
            Example example3 = new Example(WfTaskAssignUsers.class);
            example3.createCriteria().andEqualTo("mainId", taskAssign.getId());
            wfTaskAssignUsersMapper.deleteByExample(example3);
            log.info("添加用户子表信息", taskAssign.getId(), procTaskAssignInfoDTO.getUserInfos());
            insertWfTaskAssignUsers(procTaskAssignInfoDTO, taskAssign);
            //0是角色类型
        } else if (Constants.STATUS_0.equals(procTaskAssignInfoDTO.getType())) {
            log.info("删除角色子表信息，主表ID：{}", taskAssign.getId());
            Example example3 = new Example(WfTaskAssignUsers.class);
            example3.createCriteria().andEqualTo("mainId", taskAssign.getId());
            wfTaskAssignRoleMapper.deleteByExample(example3);
            log.info("添加用角色表信息", taskAssign.getId(), procTaskAssignInfoDTO.getRoleInfos());
            insertWfTaskAssignRole(procTaskAssignInfoDTO, taskAssign);
        }
    }

    private void updateWfTaskAssignUserOrRoleNew(ProcTaskAssignInfoDTO procTaskAssignInfoDTO, WfTaskAssign taskAssign) {
        deleteAllTaskAssignInfo(procTaskAssignInfoDTO, taskAssign);
        //1是用户类型
        if (StringUtils.isNotEmpty(procTaskAssignInfoDTO.getType()) && Constants.STATUS_1.equals(procTaskAssignInfoDTO.getType())) {
            log.info("添加用户子表信息", taskAssign.getId(), procTaskAssignInfoDTO.getUserInfos());
            insertWfTaskAssignUsers(procTaskAssignInfoDTO, taskAssign);
            //0是角色类型
        } else if (StringUtils.isNotEmpty(procTaskAssignInfoDTO.getType()) && Constants.STATUS_0.equals(procTaskAssignInfoDTO.getType())) {
            log.info("添加用角色表信息", taskAssign.getId(), procTaskAssignInfoDTO.getRoleInfos());
            insertWfTaskAssignRole(procTaskAssignInfoDTO, taskAssign);
        }
        //添加可驳回节点信息
        insertWfTaskNode(procTaskAssignInfoDTO, taskAssign);
    }

    /***
     * @description : 更新任务节点配置的消息订阅信息
     * @params : [procTaskAssignInfoDTO, taskAssign]
     * @returns : void
     * @author : 郭永伟
     * @date : 2020/12/14
     */
    private void updateWfTaskSubscriptionUserOrRoleNew(ProcTaskAssignInfoDTO procTaskAssignInfoDTO, WfTaskAssign taskAssign) {
        log.info("添加消息订阅用户信息,主表ID：{},用户信息：{}", taskAssign.getId(), procTaskAssignInfoDTO.getSubscriptionUserInfos());
        insertWfTaskSubscriptionUsers(procTaskAssignInfoDTO, taskAssign);
        log.info("添加消息订阅角色信息，主表ID：{},角色信息：{}", taskAssign.getId(), procTaskAssignInfoDTO.getSubscriptionRoleInfos());
        insertWfTaskSubscriptionRole(procTaskAssignInfoDTO, taskAssign);
    }

    private void insertWfTaskSubscriptionUsers(ProcTaskAssignInfoDTO procTaskAssignInfoDTO, WfTaskAssign taskAssign) {
        for (ProcTaskAssignUserInfoDTO userInfoDTO : procTaskAssignInfoDTO.getSubscriptionUserInfos()) {
            WfTaskSubscriptionUsers wfTaskSubscriptionUsers = new WfTaskSubscriptionUsers();
            wfTaskSubscriptionUsers.setMainId(taskAssign.getId());
            wfTaskSubscriptionUsers.setUserNo(userInfoDTO.getUserNo());
            wfTaskSubscriptionUsers.setUserName(userInfoDTO.getUserName());
            wfTaskSubscriptionUsers.setCreateTime(DateUtils.getCurrent());
            wfTaskSubscriptionUsers.setUpdateTime(DateUtils.getCurrent());
            wfTaskSubscriptionUsersMapper.insertSelective(wfTaskSubscriptionUsers);
        }
    }

    private void insertWfTaskSubscriptionRole(ProcTaskAssignInfoDTO procTaskAssignInfoDTO, WfTaskAssign taskAssign) {
        for (ProcTaskAssignRoleInfoDTO assignRoleInfo : procTaskAssignInfoDTO.getSubscriptionRoleInfos()) {
            WfTaskSubscriptionRole wfTaskSubscriptionRole = new WfTaskSubscriptionRole();
            wfTaskSubscriptionRole.setMainId(taskAssign.getId());
            wfTaskSubscriptionRole.setRoleName(assignRoleInfo.getRoleName());
            wfTaskSubscriptionRole.setRoleCode(assignRoleInfo.getRoleCode());
            wfTaskSubscriptionRole.setCreateTime(DateUtils.getCurrent());
            wfTaskSubscriptionRole.setUpdateTime(DateUtils.getCurrent());
            wfTaskSubscriptionRoleMapper.insertSelective(wfTaskSubscriptionRole);
        }
    }

    /***
     * @Description: 新增角色子表信息
     * @Param: [procTaskAssignInfoDTO, taskAssign]
     * @return: void
     * @Author: 郭永伟
     * @Date: 2020/8/10
     */
    private void insertWfTaskAssignRole(ProcTaskAssignInfoDTO procTaskAssignInfoDTO, WfTaskAssign taskAssign) {
        log.info("新增角色子对应的任务：{}", taskAssign);
        log.info("新增角色子：{}", procTaskAssignInfoDTO.getRoleInfos());
        for (ProcTaskAssignRoleInfoDTO assignRoleInfo : procTaskAssignInfoDTO.getRoleInfos()) {
            WfTaskAssignRole assignInfo = new WfTaskAssignRole();
            assignInfo.setMainId(taskAssign.getId());
            assignInfo.setRoleName(assignRoleInfo.getRoleName());
            assignInfo.setRoleCode(assignRoleInfo.getRoleCode());
            assignInfo.setCreateUser(procTaskAssignInfoDTO.getUserNo());
            assignInfo.setCreateTime(DateUtils.getCurrent());
            assignInfo.setUpdateUser(procTaskAssignInfoDTO.getUserNo());
            assignInfo.setUpdateTime(DateUtils.getCurrent());
            log.info("新增角色子表信息：{}", assignInfo);
            wfTaskAssignRoleMapper.insertSelective(assignInfo);
        }
    }

    /***
     * @Description: 新增用户子表信息
     * @Param: [procTaskAssignInfoDTO, taskAssign]
     * @return: void
     * @Author: 郭永伟
     * @Date: 2020/8/10
     */
    private void insertWfTaskAssignUsers(ProcTaskAssignInfoDTO procTaskAssignInfoDTO, WfTaskAssign taskAssign) {
        for (ProcTaskAssignUserInfoDTO assignUserInfo : procTaskAssignInfoDTO.getUserInfos()) {
            WfTaskAssignUsers assignInfo = new WfTaskAssignUsers();
            assignInfo.setMainId(taskAssign.getId());
            assignInfo.setUserNo(assignUserInfo.getUserNo());
            assignInfo.setUserName(assignUserInfo.getUserName());
            assignInfo.setCreateUser(procTaskAssignInfoDTO.getUserNo());
            assignInfo.setCreateTime(DateUtils.getCurrent());
            assignInfo.setUpdateUser(procTaskAssignInfoDTO.getUserNo());
            assignInfo.setUpdateTime(DateUtils.getCurrent());
            log.info("新增用户子表信息：{}", assignInfo);
            wfTaskAssignUsersMapper.insertSelective(assignInfo);
        }
    }

    /***
     * @Description: 新增可驳回节点子表信息
     * @Param: [procTaskAssignInfoDTO, taskAssign]
     * @return: void
     * @Author: 金传奇
     * @Date: 2020/8/10
     */
    private void insertWfTaskNode(ProcTaskAssignInfoDTO procTaskAssignInfoDTO, WfTaskAssign taskAssign) {
        if (CollectionUtils.isNotEmpty(procTaskAssignInfoDTO.getNodeInfos())) {
            for (ProcTaskNodeInfoDTO assignNodeInfo : procTaskAssignInfoDTO.getNodeInfos()) {
                if (assignNodeInfo != null && assignNodeInfo.getTaskDefId() != null) {
                    WfTaskAssignNode assignInfo = new WfTaskAssignNode();
                    assignInfo.setMainId(taskAssign.getId());
                    assignInfo.setNodeId(assignNodeInfo.getTaskDefId());
                    Example example = new Example(WfTaskAssign.class);
                    example.createCriteria().andEqualTo("taskDefId", assignNodeInfo.getTaskDefId());
                    assignInfo.setNodeName(assignNodeInfo.getTaskDefKey());
                    assignInfo.setCreateUser(procTaskAssignInfoDTO.getUserNo());
                    assignInfo.setCreateTime(DateUtils.getCurrent());
                    assignInfo.setUpdateUser(procTaskAssignInfoDTO.getUserNo());
                    assignInfo.setUpdateTime(DateUtils.getCurrent());
                    log.info("新增可驳回节点子表信息：{}", assignInfo);
                    wfTaskAssignNodeMapper.insertSelective(assignInfo);
                }
            }
        }

    }

    @Override
    public List<WfTaskAssign> queryProAllTaskDefInfo(String procIntId, String processDefinitionId) {
        WfOrder wf = new WfOrder();
        wf.setProcessId(procIntId);
        wf = wfOrderMapper.selectOne(wf);
        WfTaskAssign wfTaskAssign = new WfTaskAssign();
        wfTaskAssign.setProcDefKey(wf.getProcDefKey());
        wfTaskAssign.setProcDefId(processDefinitionId);
        return wfTaskAssignMapper.select(wfTaskAssign);
    }

    @Override
    public void deleteAllTaskAssignInfo(ProcTaskAssignInfoDTO procTaskAssignInfoDTO, WfTaskAssign taskAssign) {
        log.info("删除此版本流程下的任务分配信息");
        WfTaskAssign wfTaskAssign = new WfTaskAssign();
        wfTaskAssign.setProcDefId(taskAssign.getProcDefId());
        wfTaskAssign.setTaskDefId(taskAssign.getTaskDefId());
        WfTaskAssign main = wfTaskAssignMapper.selectOne(wfTaskAssign);
        if (main != null) {
            WfTaskAssignUsers userAssign = new WfTaskAssignUsers();
            userAssign.setMainId(main.getId());
            wfTaskAssignUsersMapper.delete(userAssign);
            WfTaskAssignRole roleAssign = new WfTaskAssignRole();
            roleAssign.setMainId(main.getId());
            wfTaskAssignRoleMapper.delete(roleAssign);
            WfTaskSubscriptionUsers wfTaskSubscriptionUsers = new WfTaskSubscriptionUsers();
            wfTaskSubscriptionUsers.setMainId(main.getId());
            wfTaskSubscriptionUsersMapper.delete(wfTaskSubscriptionUsers);
            WfTaskSubscriptionRole wfTaskSubscriptionRole = new WfTaskSubscriptionRole();
            wfTaskSubscriptionRole.setMainId(main.getId());
            wfTaskSubscriptionRoleMapper.delete(wfTaskSubscriptionRole);
            WfTaskAssignNode wfTaskAssignNode = new WfTaskAssignNode();
            wfTaskAssignNode.setMainId(main.getId());
            wfTaskAssignNodeMapper.delete(wfTaskAssignNode);
        }
    }

    @Override
    public List<Map<String, String>> queryPresentTaskDefInfo(ProDefDTO req) {
        List<UserTask> userTaskList = flowProcessDefinitionService.getUserTaskListById(req.getProDefId());
        log.info("userTaskList信息：", userTaskList);
        List<Map<String, String>> taskList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userTaskList)) {
            for (UserTask taskSource : userTaskList) {
                if (!req.getTaskId().equals(taskSource.getId())) {
                    Map<String, String> taskMap = new HashMap<>();
                    taskMap.put("taskDefId", taskSource.getId());
                    taskMap.put("taskDefName", taskSource.getName());
                    taskList.add(taskMap);
                }
            }
        }
        return taskList;
    }


}
